package ve.ucv.patologiaocular.showcase;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ve.ucv.patologiaocular.dao.EstudioDAO;
import ve.ucv.patologiaocular.dao.EstudioDAOImpl;
import ve.ucv.patologiaocular.domain.Estudio;
import ve.ucv.patologiaocular.util.ListaEstudio;

import com.opensymphony.xwork2.ActionSupport;

public class ListarEstudio extends ActionSupport {

	private static final long serialVersionUID = -7777983950266102889L;
	private static final Log log = LogFactory.getLog(ListarEstudio.class);
	private EstudioDAO estudioDAO = null;
	private List<ListaEstudio> gridModel;
	private List<ListaEstudio> listaEstudios;
	private List<Estudio> estudios;
	//private String id;
	private Integer rows = 0;
	private Integer page = 0;
	private Integer total = 0;
	private Integer records = 0;
	private boolean loadonce = false;
	
	public String execute() {
		log.debug("Page " + getPage()+" Rows " + getRows());
	    this.estudioDAO = new EstudioDAOImpl();
	    //if(id != null){
	    estudios = estudioDAO.listarEstudios();
    	
    	if(estudios.size() > 0){
    		listaEstudios = new ArrayList<ListaEstudio>();
    		
    		for(Estudio e : estudios){
    			listaEstudios.add(new ListaEstudio(e.getId(), e.getFechaCreado(), e.getSolicitudId().getTipoSolicitud().
    					concat("-").concat(e.getSolicitudId().getCodigo()).concat("-").concat(e.getSolicitudId().getAno()),
    					e.getDiagAnatomopat()));
    		}
    	}
		
    	setRecords(listaEstudios.size());

	    int to = (getRows() * getPage());
	    int from = to - getRows();

	    if (to > getRecords()) to = getRecords();

	    if (loadonce)
	    {
	      setGridModel(listaEstudios);
	    }
	    else
	    {
	    	 setGridModel(listaEstudios.subList(from, to));
	    }
	    
	    setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));
    	
		return SUCCESS;
	}
	
	public String getJSON()
	{
	    return execute();
	}

	public EstudioDAO getEstudioDAO() {
		return estudioDAO;
	}

	public void setEstudioDAO(EstudioDAO estudioDAO) {
		this.estudioDAO = estudioDAO;
	}

	public List<ListaEstudio> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<ListaEstudio> gridModel) {
		this.gridModel = gridModel;
	}

	public List<ListaEstudio> getListaEstudios() {
		return listaEstudios;
	}

	public void setListaEstudios(List<ListaEstudio> listaEstudios) {
		this.listaEstudios = listaEstudios;
	}

	public List<Estudio> getEstudios() {
		return estudios;
	}

	public void setEstudios(List<Estudio> estudios) {
		this.estudios = estudios;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
		this.records = records;
		
		if (this.records > 0 && this.rows > 0)
	    {
	      this.total = (int) Math.ceil((double) this.records / (double) this.rows);
	    }
	    else
	    {
	      this.total = 0;
	    }
	}

	public boolean isLoadonce() {
		return loadonce;
	}

	public void setLoadonce(boolean loadonce) {
		this.loadonce = loadonce;
	}
}
